<?php
/**
 * Created by 团购类.
 * User: 尚金殿
 * Date: 2018/8/29
 * Time: 14:39
 */

namespace app\admin\controller;


use think\Db;

class CollageController extends CommonController
{
    protected $mysql = 'collage';
    protected $pk = 'collage_id';
    protected $status = 'collage_status';


    //加载列表
    public function lists(){
        //获取时间戳
        $startTime = strtotime($this->request->param('start_time'));
        $endTime = strtotime($this->request->param('end_time'));
        //获取手机号
        $phone = $this->request->param('phone');

        //判定数据
        if($phone){
           //根据手机号查询
           $res = Db::name('collage_member')->alias('m')->join('t_collage c','m.collage_id = c.collage_id')->join('t_line l','c.ware_id = l.id')->join('t_user u','m.user_id = u.id')->where('m.member_status','>',-1)->paginate(10);
        }else if($startTime){
            //根据时间查询
            $res = Db::name('collage_member')->alias('m')->join('t_collage c','m.collage_id = c.collage_id')->join('t_line l','c.ware_id = l.id')->join('t_user u','m.user_id = u.id')->where('m.create_time','>',$startTime)->where('m.update_time','<',$endTime)->where('m.member_status','>',-1)->paginate(10);
        }else{
            $res = Db::name('collage_member')->alias('m')->join('t_collage c','m.collage_id = c.collage_id')->join('t_line l','c.ware_id = l.id')->join('t_user u','m.user_id = u.id')->where('m.member_status','>',-1)->paginate(10);
        }
        $this->assign(['startTime'=>$this->request->param('start_time'),'endTime'=>$this->request->param('end_time'),'res'=>$res,'phone'=>$phone]);
        return $this->fetch();
    }

    //删除数据(修改状态)
    public function setStatus(){
        $id = $this->request->param('id');
        $data['member_status'] = $this->request->param('status');
        $data['update_time'] = time();

        $res = Db::name('collage_member')->where('member_id',$id)->update($data);

        if(!$res){
            return show(0,'操作失败');
        }
        return show(1,'操作成功');
    }


    //退款操作
    public function edit(){
        //获取团员ID
        $id = $this->request->param('id');

        //根据团员表ID查询团购订单ID
        $member = Db::name('collage_member')->where('member_id',$id)->find();
        //根据团购订单查询团购价格
        $collage = Db::name($this->mysql)->where($this->pk,$member['collage_id'])->find();
        //根据用户ID获取余额
        $user = Db::name('user')->where('id',$member['user_id'])->find();

        //修改用户余额
        $res['balance'] = $user['balance'] + $collage['collage_price'];
        $result = Db::name('user')->where('id',$user['id'])->update($res);
        //用户余额修改成功后修改团购成员状态
        if(!$result){
            echo "<script>alert('用户余额修改失败');location.href='/admin.php/collage/lists'</script>";
        }
        $arr['member_status'] = 2;
        $arr['update_time'] = time();
        $str = Db::name('collage_member')->where('member_id',$id)->update($arr);
        if(!$str){
            echo "<script>alert('团购成员表修改失败');location.href='/admin.php/collage/lists'</script>";
        }
        echo "<script>alert('退款成功');location.href='/admin.php/collage/lists'</script>";
    }


    
}